package org.license.test.verify;

import de.schlichtherle.license.*;
import de.schlichtherle.util.ObfuscatedString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;

/**
 * Created by jinshaowei on 2017/9/21.
 */
public class MacLicenseManager extends LicenseManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(MacLicenseManager.class);
    
    /** => "exc.licenseHasExpired" */
    private static final String EXC_LICENSE_HAS_EXPIRED = new ObfuscatedString(new long[] {
            0xDE2B2A7ACD6DA6DL, 0x9EE12DDECB3D4C0DL, 0xB3CF760B522E8688L,
            0x316BD3E92C17CC40L
    }).toString();


    /**
     * Constructs a License Manager.
     * <p>
     * <b>Warning:</b> The manager created by this constructor is <em>not</em>
     * valid and cannot be used unless {@link #setLicenseParam(LicenseParam)}
     * is called!
     */
    protected MacLicenseManager() {
    }

    /**
     * Constructs a License Manager.
     *
     * @param  param the license configuration parameters
     *         - may <em>not</em> be {@code null}.
     * @throws NullPointerException If the given parameter object does not
     *         obey the contract of its interface due to a {@code null}
     *         pointer.
     * @throws IllegalPasswordException If any password in the parameter object
     *         does not comply to the current policy.
     */
    public MacLicenseManager(LicenseParam param) {
        super.setLicenseParam(param);
    }

    @Override
    protected synchronized void validate(LicenseContent content) throws LicenseContentException {
        super.validate(content);
        String macAddress = content.getExtra().toString();

        try {
            if (!validateMacAddress(macAddress)) {
                throw new LicenseContentException(EXC_LICENSE_HAS_EXPIRED);
            }
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            throw new LicenseContentException(EXC_LICENSE_HAS_EXPIRED);
        }

    }

    public static void main(String args[]) throws SocketException {
        String mac = "3C-15-C2-D8-8B-16";
        boolean macflag = validateMacAddress( mac);
        LOGGER.debug("validatoMacAddress flag=%s\n", macflag);
    }


    public static boolean validateMacAddress(String macAddress)
            throws SocketException {
        boolean returnFlag = false;
        Enumeration<NetworkInterface> nets = NetworkInterface
                .getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets)) {
            byte[] mac = netint.getHardwareAddress();
            StringBuilder sb = new StringBuilder();
            if (mac != null) {
                for (int i = 0; i < mac.length; i++) {
                    sb.append(String.format("%02X%s", mac[i],
                            (i < mac.length - 1) ? "-" : ""));
                }
                LOGGER.debug("mac=" + sb.toString());
            }
            if (sb.toString().equals(macAddress)) {
                returnFlag = true;
            }
        }
        return returnFlag;
    }

}
